//全局变量
var activeId;
ajax({
	url:"http://www.antmall.cc/public/wx/collect/data/"+activeId+".html",
	success:function(data){
		var info = JSON.parse(data);
		var oImgs=document.getElementsByClassName("left");
		var gifts=info.data.gifts;
		var titles=document.getElementsByClassName("title");
		var price_nums=document.getElementsByClassName("price_num");
		var total_nums=document.getElementsByClassName("totalNum");
		var start_time=document.getElementById("startTime");
		var end_time=document.getElementById("endTime");
		var start_times=start_time.getElementsByTagName("i");
		var end_times=end_time.getElementsByTagName("i");
		var arr_start=info.data.active.base.startTime;
		var arr_end=info.data.active.base.endTime;
		var active_id=info.data.active.base.id;
		var arr=[];
		var date_end = new Date(arr_end.replace(/-/g,"/"));
		var date_start = new Date(arr_start.replace(/-/g,"/"));
		var index_goods_buttons=document.getElementsByClassName("index_goods_button");
		var b=false;
		var info_button=document.getElementById("info_button");
		var info_cover=document.getElementsByClassName("info_cover")[0];

		function timer(){
			var ts = (new Date(date_end)) -new Date();
            var dd = parseInt(ts / 1000 / 60 / 60 / 24, 10);//计算剩余的天数   x
            var hh = parseInt(ts / 1000 / 60 / 60 % 24, 10);//计算剩余的小时数  
            var mm = parseInt(ts / 1000 / 60 % 60, 10);//计算剩余的分钟数  
            var ss = parseInt(ts / 1000 % 60, 10);//计算剩余的秒数  
            dd = checkTime(dd);  
            hh = checkTime(hh);  
            mm = checkTime(mm);  
            ss = checkTime(ss); 
            document.getElementById("day").innerHTML = dd; 
            document.getElementById("hour").innerHTML = hh;
            document.getElementById("min").innerHTML = mm;
            document.getElementById("sec").innerHTML = ss; 
            function checkTime(i){    
               if (i < 10) {    
                   i = "0" + i;    
                }    
               return i;    
            }
        }
        timer();
        setInterval(timer, 1000)

		for(let i=0;i<oImgs.length;i++){
	    	oImgs[i].onclick=function(){
	        	window.location.href="goodsinfo.html";

    		}
		}
		for(let j=0;j<index_goods_buttons.length;j++){
			index_goods_buttons[j].index=j;
			index_goods_buttons[j].onclick=function(){
				for(var i=0;i<index_goods_buttons.length;i++){
					index_goods_buttons[i].style.backgroundImage ="url(images/goods_button.png)";
				}
				this.style.backgroundImage ="url(images/selected.png)";
				b=true;
				var index_button=document.getElementById("index_button");
				if(b){
					index_button.onclick=function(){
				 		info_cover.style.display = "block";

						info_cover.onclick=function(){
							info_cover.style.display = "none";
						};
						var info=document.getElementsByClassName("info")[0];
							info.onclick=function(e){
								e.stopPropagation();
							}

					}
				
				}
				var reg = /^1[0-9]{10}$/;
				info_button.onclick=function(){
				var userName=document.getElementsByClassName("username")[0].value;
				var userPhone=document.getElementsByClassName("userphone")[0].value;
					if(reg.test(userPhone)){
						ajax({
							url:"http://www.antmall.cc/public/wx/collect/data/checkSign.html?activeId=10135",
					
							success:function(data){
								var data = JSON.parse(data);
								if(!data.code){
									var req = {"activeId":info.data.active.base.id,"userName":userName,"userPhone":userPhone,"giftId":gifts[j].id};
									$.post("http://www.antmall.cc/public/wx/collect/data/sign.html",req,function(res){
										var res = $.parseJSON(res);
										var mppId=res.data.mppId;
											window.location.href="participate.html";
									});
								}else {
									console.log("已参加活动")
								}
							}
						})	
					}else {
					console.log("请输入正确的手机号");
					}
				}
				

			}

			if(!b){
				var prompt_cover=document.getElementsByClassName("prompt_cover")[0];
				index_button.onclick=function(e){
					document.body.scrollTop = 500;
					prompt_cover.style.display = "block";
					setTimeout(
						function(){
							prompt_cover.style.display = "none";
					},1000)
					
			
				}
			}
		}

		start_times[0].innerHTML=date_start.getFullYear();
		start_times[1].innerHTML=date_start.getMonth()+1;
		start_times[2].innerHTML=date_start.getDate();
		
		end_times[0].innerHTML=date_end.getFullYear();
		end_times[1].innerHTML=date_end.getMonth()+1;
		end_times[2].innerHTML=date_end.getDate();

		for(var i=0;i<gifts.length;i++){
			oImgs[i].style.backgroundImage = "url("+gifts[i].giftImg+")"; 
			titles[i].innerHTML=gifts[i].giftTitle;
			price_nums[i].innerHTML=(gifts[i].marketPrice)/100;
			total_nums[i].innerHTML=gifts[i].totalNum;

		}
	
			
	}
})


function ajax(info){
	if(window.XMLHttpRequest){
		var xhr = new XMLHttpRequest();	
	}else{
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = info.url;
	var type = info.type||"GET";
	var data = info.data||"";
	
	if(type=="GET"){
		xhr.open("GET",url,true);
		xhr.send();
	}else{
		xhr.open("POST",url,true);
		xhr.setRequestHeader("myHeader","myvalue");
		xhr.send(data);
	}
	
	xhr.onload = function(){
		if(info.success){
			info.success(xhr.responseText);
		}
	}
}
